package com.softeware.dao.user;

import com.softeware.dao.BaseDao;
import com.softeware.pojo.UserInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao{
    @Override
    public int add(Connection connection, UserInfo user) throws Exception {
        int flag = 0;
        PreparedStatement pstm = null;
        if(connection != null){
            String sql = "insert into users (username,password) values(?,?)";
            Object[] params = {user.getUsername(),user.getPassword()};
            flag = BaseDao.execute(connection,pstm,sql,params);
            BaseDao.closeResource(null,pstm,null);
        }
        return flag;
    }

    @Override
    public UserInfo getLoginUser(Connection connection, String username) throws Exception {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        UserInfo user =null;
        if(connection != null){
            String sql = "select * from users where username=?";
            Object[] params = {username};
            rs = BaseDao.execute(connection,pstm,rs,sql,params);
            if(rs.next()){
                user = new UserInfo();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
            BaseDao.closeResource(null,pstm,rs);
        }
        return user;
    }
}
